package com.hhsjdesign.dynamicui.core.widgetimpl.businesswidget

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hhsjdesign.dynamicui.annoprocess.AWidgetType
import com.hhsjdesign.dynamicui.core.widgetimpl.BusinessWidget
import com.hhsjdesign.dynamicui.interfaces.RequestListener
import com.hhsjdesign.dynamicui.businessdata.BaseModel
import com.hhsjdesign.dynamicui.businessdata.IViewable
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetData
import com.hhsjdesign.dynamicui.core.property.widget.GridVWidgetProperty
import com.hhsjdesign.dynamicui.core.property.WidgetSpecProperty
import com.hhsjdesign.dynamicui.core.widgetimpl.businesswidget.adapter.RecyclerSimpleAdapter
import com.hhsjdesign.dynamicui.core.widgetimpl.businesswidget.adapter.WidgetHolder
import com.hhsjdesign.dynamicui.core.property.viewdata.ItemViewData
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetTypeConst

//VerticalGridLayoutProperty
@AWidgetType(WidgetTypeConst.GRID_VERTICAL)
class GridVWidget constructor(
    context: Context,
    data: WidgetData
) : BusinessWidget(context, data) {
    lateinit var recyclerView: RecyclerView
    var adapter: RecyclerSimpleAdapter? = null

    override fun createMainView(specProperty: WidgetSpecProperty): View {
        recyclerView = RecyclerView(context).apply {
            isNestedScrollingEnabled = false;
        }
        return recyclerView
    }

    override fun loadLocalBusiness() {
        if (adapter == null) {
            adapter = GridHWidget.HorizontalGridAdapter(
                context,
                widgetData.business!!,
                widgetData.itemPropertyList!![0].itemWidgetType,
                widgetData.itemPropertyList!![0].itemPropertyModel,
                widgetData.businessData!!,
                0,
                (widgetData.specProperty as GridVWidgetProperty).itemHeight,
                widgetData.viewProperty!!.size[2]
            )
        }
        setAdapter()

    }

    override fun loadRemoteBusiness() {
        dataCaller?.loadData(
            widgetData.business!!,
            widgetData.params!!,
            this,
            object : RequestListener<Any> {
                override fun onRequestSuccess(requestId: String, result: BaseModel<Any>) {
                    if (result.type == 1) {
                        recyclerView.layoutManager = GridLayoutManager(
                            context,
                            (widgetData.specProperty!! as GridVWidgetProperty).columnNumber,
                            GridLayoutManager.VERTICAL,
                            false
                        )
                        VerticalGridAdapter(
                            context,
                            widgetData.business!!,
                            widgetData.itemPropertyList!![0].itemWidgetType,
                            widgetData.itemPropertyList!![0].itemPropertyModel,
                            result.toIViewableList() as MutableList<IViewable<*>>,
                            0,
                            (widgetData.specProperty!! as GridVWidgetProperty).itemHeight,
                            widgetData.viewProperty!!.size[2]
                        ).let {
                            recyclerView.adapter = it
                            it.notifyDataSetChanged()
                        }
                    }
                }

                override fun onRequestFail(
                    requestId: String,
                    resultCode: Int,
                    resultMessage: String
                ) {
                }
            }
        )
    }

    private fun setAdapter() {
        if (adapter != null && recyclerView != null) {
            recyclerView.adapter = adapter
        }
    }

    open class VerticalGridAdapter constructor(
        context: Context,
        business: String,
        itemWidgetType: String?,
        viewWidget: ItemViewData?,
        itemDataList: MutableList<IViewable<*>>,
        itemWidth: Int,
        itemHeight: Int,
        parentHeightType: Int
    ) : RecyclerSimpleAdapter(
        context,
        business,
        itemWidgetType,
        viewWidget,
        itemDataList,
        itemWidth,
        itemHeight,
        parentHeightType
    ) {

        override fun onBindViewHolder(holder: WidgetHolder<*>, position: Int) {
            super.onBindViewHolder(holder, position)
            holder.itemView.layoutParams.height = itemHeight
        }
    }
}